package com.zlm.qsmanage.component;

import com.alibaba.fastjson.JSONObject;
import com.zlm.qsmanage.entity.Bighouse;
import com.zlm.qsmanage.entity.Collect;
import com.zlm.qsmanage.mapper.BighouseMapper;
import com.zlm.qsmanage.mapper.CollectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArraySet;

@Component
@ServerEndpoint("/websocket/")  // 注意不要以'/'结尾
public class WebSocketServer {//整个WebSocket就是一个类，每来一个连接就创建一个线程创建一个类



//    final private static ConcurrentHashMap<Integer, WebSocketServer> bots = new ConcurrentHashMap<>();
//    final private static CopyOnWriteArraySet<Bighouse> matchpool = new CopyOnWriteArraySet<>();
//    private Bighouse bot;
//    private Collect collect;
    private Session session = null;   //每个连接用session维护的
//    private static CollectMapper collectMapper;
//    private static BighouseMapper bighouseMapper;

//    @Autowired
//    public void setBighouseMapper(BighouseMapper bighouseMapper) {
//        WebSocketServer.bighouseMapper = bighouseMapper;
//    }



    @OnOpen
   // public void onOpen(Session session, @PathParam("id") String id) throws IOException {
        public void onOpen(Session session) throws IOException {
        this.session = session;
        System.out.println("connected!");
//        Integer bot_id = Integer.valueOf(id);
//        this.bot = bighouseMapper.selectById(bot_id);
//
//        if (this.bot != null) {
//            bots.put(bot_id, this);
//        } else {
//            this.session.close();
//        }
//
//        System.out.println(bots);
    }

    @OnClose
    public void onClose() {
        System.out.println("disconnected!");
//        if (this.bot != null) {
//            bots.remove(this.bot.getId());
//            matchpool.remove(this.bot);
//        }
    }


    @OnMessage
    public void onMessage(String message, Session session) {  // 当做路由
        System.out.println(message);
        System.out.println("receive message!");
        String event = message;
        if ("add".equals(event)){
            System.out.println("向前端传入平均数据");
            Random random = new Random();
            int temperature_15 = random.nextInt(10)+25;
            int humidity_15 = random.nextInt(30)+40;
            int ammonia_concentration_15 = random.nextInt(15)+5;
            int light_15 = random.nextInt(3)+14;

            int temperature_day = random.nextInt(10)+25;
            int humidity_day = random.nextInt(30)+40;
            int ammonia_concentration_day = random.nextInt(15)+5;
            int light_day = random.nextInt(3)+14;

            int temperature_month = random.nextInt(10)+25;
            int humidity_month = random.nextInt(30)+40;
            int ammonia_concentration_month = random.nextInt(15)+5;
            int light_month = random.nextInt(3)+14;

            int temperature_year = random.nextInt(10)+25;
            int humidity_year = random.nextInt(30)+40;
            int ammonia_concentration_year = random.nextInt(15)+5;
            int light_year = random.nextInt(3)+14;

            JSONObject resp =new JSONObject();
            resp.put("temperature_15",temperature_15);
            resp.put("humidity_15",humidity_15);
            resp.put("ammonia_concentration_15",ammonia_concentration_15);
            resp.put("light_15",light_15);

            resp.put("temperature_day",temperature_day);
            resp.put("humidity_day",humidity_day);
            resp.put("ammonia_concentration_day",ammonia_concentration_day);
            resp.put("light_day",light_day);

            resp.put("temperature_month",temperature_month);
            resp.put("humidity_month",humidity_month);
            resp.put("ammonia_concentration_month",ammonia_concentration_month);
            resp.put("light_month",light_month);

            resp.put("temperature_year",temperature_year);
            resp.put("humidity_year",humidity_year);
            resp.put("ammonia_concentration_year",ammonia_concentration_year);
            resp.put("light_year",light_year);
            sendMessage(resp.toJSONString());
        }
        if (event == null)
            System.out.println("请求错误");
    }

    @OnError
    public void onError(Session session, Throwable error) {
        error.printStackTrace();
    }

    public void sendMessage(String message) {
        synchronized (this.session) {
            try {
                this.session.getBasicRemote().sendText(message);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
